package convention;

import java.util.concurrent.Semaphore;

public class PrintABCUsingSemaphore {
    private static Semaphore semaphoreA = new Semaphore(1);
    private static Semaphore semaphoreB = new Semaphore(0);
    private static Semaphore semaphoreC = new Semaphore(0);

    public static void main(String[] args) {
        PrintABCUsingSemaphore printer = new PrintABCUsingSemaphore();

        new Thread(() -> printer.print("A", semaphoreA, semaphoreB), "A").start();

        new Thread(() -> printer.print("B", semaphoreB, semaphoreC), "B").start();

        new Thread(() -> printer.print("C", semaphoreC, semaphoreA), "C").start();
    }

    private void print(String name, Semaphore current, Semaphore next) {
        try {
            for (int i = 0; i < 10; i++) {
                current.acquire();
                System.out.print(name);
                next.release();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}